import wootConstants from '../constants/wootConstants'

export default {
  methods: {
    setFilterAttributes () {
      const allCustomAttributes = this.$store.getters[
        'attributes/getAttributesByModel'
      ](this.attributeModel)
      const customAttributesFormatted = {
        name: this.$t(`${this.filtersFori18n}.GROUPS.CUSTOM_ATTRIBUTES`),
        attributes: allCustomAttributes.map(attr => {
          return {
            key: attr.attributeKey,
            name: attr.attributeDisplayName
          }
        })
      }
      const allFilterGroups = this.filterAttributeGroups.map(group => {
        return {
          name: this.$t(`${this.filtersFori18n}.GROUPS.${group.i18nGroup}`),
          attributes: group.attributes.map(attribute => {
            return {
              key: attribute.key,
              name: this.$t(
                `${this.filtersFori18n}.ATTRIBUTES.${attribute.i18nKey}`
              )
            }
          })
        }
      })
      const customAttributeTypes = allCustomAttributes.map(attr => {
        return {
          attributeKey: attr.attributeKey,
          attributeI18nKey: `CUSTOM_ATTRIBUTE_${attr.attributeDisplayType.toUpperCase()}`,
          inputType: this.customAttributeInputType(attr.attributeDisplayType),
          filterOperators: this.getOperatorTypes(attr.attributeDisplayType),
          attributeModel: 'customAttributes'
        }
      })
      this.filterTypes = [...this.filterTypes, ...customAttributeTypes]
      this.filterGroups = [...allFilterGroups, customAttributesFormatted]
    },

    initializeExistingFilterToModal () {
      this.initializeStatusAndAssigneeFilterToModal()
      this.initializeInboxTeamAndLabelFilterToModal()
    },
    initializeStatusAndAssigneeFilterToModal () {
      if (this.activeStatus !== '') {
        this.appliedFilter.push({
          attributeKey: 'status',
          attributeModel: 'standard',
          filterOperator: 'equal_to',
          values: [
            {
              id: this.activeStatus,
              name: this.$t(
                `CHAT_LIST.CHAT_STATUS_FILTER_ITEMS.${this.activeStatus}.TEXT`
              )
            }
          ],
          queryOperator: 'and',
          customAttributeType: ''
        })
      }
      if (this.activeAssigneeTab === wootConstants.ASSIGNEE_TYPE.ME) {
        this.appliedFilter.push({
          attributeKey: 'assignee_id',
          filterOperator: 'equal_to',
          values: this.currentUserDetails,
          queryOperator: 'and',
          customAttributeType: ''
        })
      }
    },
    initializeInboxTeamAndLabelFilterToModal () {
      if (this.conversationInbox) {
        this.appliedFilter.push({
          attributeKey: 'inboxId',
          attributeModel: 'standard',
          filterOperator: 'equal_to',
          values: [
            {
              id: this.conversationInbox,
              name: this.inbox.name
            }
          ],
          queryOperator: 'and',
          customAttributeType: ''
        })
      }
      if (this.teamId) {
        this.appliedFilter.push({
          attributeKey: 'teamId',
          attributeModel: 'standard',
          filterOperator: 'equal_to',
          values: this.activeTeam,
          queryOperator: 'and',
          customAttributeType: ''
        })
      }
      if (this.label) {
        this.appliedFilter.push({
          attributeKey: 'labels',
          attributeModel: 'standard',
          filterOperator: 'equal_to',
          values: [
            {
              id: this.label,
              name: this.label
            }
          ],
          queryOperator: 'and',
          customAttributeType: ''
        })
      }
    }
  }
}
